package cn.school.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.school.mybatis.pojo.Student;
import cn.school.mybatis.service.StudentService;

/**
 * Servlet implementation class StudentServlet
 */
@WebServlet("/studentServlet")
public class StudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StudentServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	StudentService service = new StudentService();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//		 Admin _name=(Admin)session.getAttribute("USER_LOGIN");
//			if(_name==null){
//				//response.sendRedirect("userLogin.jsp");//   
//				out.print("<script>alert('您还没有登录,不能访问');location.replace('"+request.getContextPath()+"/pages/userLogin.jsp');</script>");
//				return;
//			}

		request.setCharacterEncoding("utf-8");
		int pageNo = 1;
		int pageSize = 10;
		try {
			pageNo = Integer.parseInt(request.getParameter("p"));
		} catch (Exception e) {
		}

		Map<String, Object> map = new HashMap<>();
		String sname = request.getParameter("sname");
		String telephone = request.getParameter("telephone");
		sname = sname == null ? "" : sname;
		telephone = telephone == null ? "" : telephone;
		map.put("sname", sname);
		map.put("telephone", telephone);

		List<Student> list = service.search0(map, pageNo, pageSize, "stuno asc");
		int totalPage = service.searchTotal(map, pageSize);

		// 哪些值需要传递给jsp
		request.setAttribute("pageNo", pageNo);
		request.setAttribute("sname", sname);
		request.setAttribute("telephone", telephone);
		request.setAttribute("totalPage", totalPage);
		request.setAttribute("list", list);

		// request.getRequestDispatcher("/page2/student.jsp").forward(request,
		// response);
		request.getRequestDispatcher("/WEB-INF/jsp/student.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
